<?php

namespace App\Hospital\Controllers;

use App\Hospital\Repositories\Hospital;
use App\Models\Hospital as ModelsHospital;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class HospitalController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Hospital(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('province');
            $grid->column('county');
            $grid->column('city');
            $grid->column('longitude');
            $grid->column('latitude');
            $grid->column('intro');
            $grid->column('phone');
            $grid->column('clinic_time');
            $grid->column('address');
            $grid->column('is_family');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Hospital(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('province');
            $show->field('county');
            $show->field('city');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('intro');
            $show->field('phone');
            $show->field('clinic_time');
            $show->field('address');
            $show->field('is_family');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $hospital_id = Admin::user()->hospital_id;
        return Form::make( new Hospital(), function (Form $form) {
            $form->display('name');
            $form->text('phone')->required();
            $form->textarea('intro')->default("为了宝宝享受更好的健康服务我们一起加油")->required();
            $form->textarea('clinic_time')->required();
            $form->text('address')->required();
            $form->text('code','中心编码')->required();
            $form->text('precinct','所属派出所')->required();
            $form->text('post_code','邮编')->required();
            $form->text('street','街道')->required();
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->disableViewCheck();
            $form->disableListButton();
            //$form->defaultEditingChecked();

//            $form->hasMany('links', function (Form\NestedForm $form) {
//                $form->text('name')->label('名称');
//                $form->url('link')->label('链接');
//                //$form->hidden('hospital_id')->value(4);
//            })->label('首页快捷链接')->useTable()->saving(function ($value) {
//                return json_encode($value);
//            });
//            // 跳转并提示成功信息
//            $form->saved(function (Form $form) {
//                return $form->response()->success('保存成功')->redirect('hospital-view/create');
//            });
            $form->saved(function (Form $form) {
                return $form->response()->success('保存成功')->redirect('hospital-view/create');
            });
        })->edit($hospital_id)->action('hospital-view/'.$hospital_id);
    }

}
